package businessLogic;

import businessEntities.ColumnsInformationContainer;
import dataAccess.DALInformationQuery;
import businessEntities.DBInformationContainer;
import businessEntities.exceptions.ColumnsQueryException;
import businessEntities.exceptions.ConnectionException;
import businessEntities.exceptions.InformationQueryException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class BLInformationFinder {

    /**
     * Returns Map. The Keys are the databases/schemas names inside the data base.
     * Each value is a list of all the tables inside a Database/schema.
     * Example Map.getKey("College") returns the list of all the tables inside the 'College' database.
     * @return Map<String, List<String>> or <code>null</code> if error was occurred during the process.
     */
    public Map<String, List<String>> getDbInformation() {

        Map<String, List<String>> result = new HashMap<>();
        DALInformationQuery DbInformation = new DALInformationQuery();

        try {
            DBInformationContainer dbInfContainer = DbInformation.getInformation();

            List<String> Dbs = dbInfContainer.getDbsCopy();
            List<String> Tables = dbInfContainer.getTablesCopy();
            int listSize = dbInfContainer.getDbsCopy().size();

            for (int i = 0; i < listSize; i++) {
                String Db = Dbs.get(i);
                String table = Tables.get(i);

                if (result.containsKey(Db)) {
                    List<String> list = result.get(Db);
                    list.add(table);
                } else {
                    List<String> list = new ArrayList<>();
                    list.add(table);
                    result.put(Db, list);
                }
            }

        } catch (InformationQueryException | ConnectionException ex) {
            return null;
        }
        return result;
    }

    /**
     * @param dataBase
     * @param table
     * @return ColumnsInformationContainer that holds the information of all the columns inside specific table. returns <code>null</code> for any exception.
     */
    public ColumnsInformationContainer getColumns(String dataBase, String table) {
        ColumnsInformationContainer result = null;
        DALInformationQuery columnsInformation = new DALInformationQuery();
        try {
            result = columnsInformation.getColumnsInformation(dataBase, table);
        } catch (ColumnsQueryException | ConnectionException ex) {
            return null;
        }
        return result;
    }
}
